package com.ssy.lingxi.platform.manage.serviceimpl.paas;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.manage.constant.CommonBooleanEnum;
import com.ssy.lingxi.platform.manage.entity.base.OnlineService;
import com.ssy.lingxi.platform.manage.model.request.common.CommonIdRequest;
import com.ssy.lingxi.platform.manage.model.request.onlineservice.EnableStatusReq;
import com.ssy.lingxi.platform.manage.model.request.onlineservice.OnlineServiceAddReq;
import com.ssy.lingxi.platform.manage.model.request.onlineservice.OnlineServiceUpdateReq;
import com.ssy.lingxi.platform.manage.repository.OnlineServiceRepository;
import com.ssy.lingxi.platform.manage.service.paas.IOnlineServicePASSService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 基础设置-在线客服实现类
 * @author wyx
 * @version 2.0.0
 * @date 2021-11-16
 */
@Service
public class OnlineServicePASSServiceImpl implements IOnlineServicePASSService {
    @Resource
    private OnlineServiceRepository onlineServiceRepository;


    /**
     * 新增客服
     * @param req 请求参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> add(OnlineServiceAddReq req) {
        OnlineService onlineService = new OnlineService();
        onlineService.setType(req.getType());
        onlineService.setParamConfigList(req.getParamConfigList());
        onlineService.setPlatformName(req.getPlatformName());
        onlineService.setStatus(CommonBooleanEnum.NO.getCode());
        onlineServiceRepository.save(onlineService);
        return Wrapper.success();
    }

    /**
     * 修改客服
     * @param req 请求参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> update(OnlineServiceUpdateReq req) {
        OnlineService onlineService = onlineServiceRepository.findById(req.getId()).orElse(null);
        if (onlineService == null) {
            return Wrapper.fail(ResponseCode.SYSTEM_RECORDS_DON_T_EXIST);
        }
        onlineService.setType(req.getType());
        onlineService.setParamConfigList(req.getParamConfigList());
        onlineService.setPlatformName(req.getPlatformName());
        onlineServiceRepository.save(onlineService);
        return Wrapper.success();
    }

    /**
     * 启用/停用
     * @param req 请求参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> enable(EnableStatusReq req) {
        OnlineService onlineService = onlineServiceRepository.findById(req.getId()).orElse(null);
        if (onlineService == null) {
            return Wrapper.fail(ResponseCode.SYSTEM_RECORDS_DON_T_EXIST);
        }
        onlineService.setStatus(req.getStatus());
        onlineServiceRepository.save(onlineService);
        return Wrapper.success();
    }

    /**
     * 删除客服
     * @param req 请求参数
     * @return 返回结果
     */
    @Override
    public Wrapper<Void> delete(CommonIdRequest req) {
        onlineServiceRepository.deleteById(req.getId());
        return Wrapper.success();
    }
}
